<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Generate signature</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.openssl-sign" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_sign</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">openssl_sign</span> &mdash; <span class="dc-title">Generate signature</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.openssl-sign-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>openssl_sign</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$data</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$signature</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$priv_key_id</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$signature_alg</code><span class="initializer"> = OPENSSL_ALGO_SHA1</span></span>
  ] ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>openssl_sign()</strong></span> computes a signature for the
   specified <code class="parameter">data</code> by generating a cryptographic
   digital signature using the private key associated with
   <code class="parameter">priv_key_id</code>. Note that the data itself is
   not encrypted.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-sign-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">data</code></dt>

     <dd>

      <p class="para">
       The string of data you wish to sign
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">signature</code></dt>

     <dd>

      <p class="para">
       If the call was successful the signature is returned in
       <code class="parameter">signature</code>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">priv_key_id</code></dt>

     <dd>

      <p class="para">
       <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> _ a key, returned by <span class="function"><a href="openssl_get_privatekey.html" class="function">openssl_get_privatekey()</a></span>
      </p>
      <p class="para">
       <span class="type"><a href="language.types.string.html" class="type string">string</a></span> - a PEM formatted key
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">signature_alg</code></dt>

     <dd>

      <p class="para">
       <span class="type"><a href="language.types.integer.html" class="type int">int</a></span> _ one of these <a href="openssl.signature_algos.html" class="link">Signature Algorithms</a>.
      </p>
      <p class="para">
       <span class="type"><a href="language.types.string.html" class="type string">string</a></span> _ a valid string returned by <span class="function"><a href="openssl_get_md_methods.html" class="function">openssl_get_md_methods()</a></span> example, &quot;sha256WithRSAEncryption&quot; or &quot;sha384&quot;.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-sign-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.openssl-sign-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-945">
    <p><strong>Example #1 <span class="function"><strong>openssl_sign()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;$data&nbsp;is&nbsp;assumed&nbsp;to&nbsp;contain&nbsp;the&nbsp;data&nbsp;to&nbsp;be&nbsp;signed<br /><br />//&nbsp;fetch&nbsp;private&nbsp;key&nbsp;from&nbsp;file&nbsp;and&nbsp;ready&nbsp;it<br /></span><span style="color: #0000BB">$pkeyid&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_pkey_get_private</span><span style="color: #007700">(</span><span style="color: #DD0000">"file://src/openssl-0.9.6/demos/sign/key.pem"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;compute&nbsp;signature<br /></span><span style="color: #0000BB">openssl_sign</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$signature</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pkeyid</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;free&nbsp;the&nbsp;key&nbsp;from&nbsp;memory<br /></span><span style="color: #0000BB">openssl_free_key</span><span style="color: #007700">(</span><span style="color: #0000BB">$pkeyid</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
   <div class="example" id="example-946">
    <p><strong>Example #2 <span class="function"><strong>openssl_sign()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//data&nbsp;you&nbsp;want&nbsp;to&nbsp;sign<br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'my&nbsp;data'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//create&nbsp;new&nbsp;private&nbsp;and&nbsp;public&nbsp;key<br /></span><span style="color: #0000BB">$new_key_pair&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_pkey_new</span><span style="color: #007700">(array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"private_key_bits"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2048</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"private_key_type"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">OPENSSL_KEYTYPE_RSA</span><span style="color: #007700">,<br />));<br /></span><span style="color: #0000BB">openssl_pkey_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$new_key_pair</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$private_key_pem</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$details&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_pkey_get_details</span><span style="color: #007700">(</span><span style="color: #0000BB">$new_key_pair</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$public_key_pem&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$details</span><span style="color: #007700">[</span><span style="color: #DD0000">'key'</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">//create&nbsp;signature<br /></span><span style="color: #0000BB">openssl_sign</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$signature</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$private_key_pem</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">OPENSSL_ALGO_SHA256</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//save&nbsp;for&nbsp;later<br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'private_key.pem'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$private_key_pem</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'public_key.pem'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$public_key_pem</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'signature.dat'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$signature</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//verify&nbsp;signature<br /></span><span style="color: #0000BB">$r&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_verify</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$signature</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$public_key_pem</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"sha256WithRSAEncryption"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$r</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
 
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.openssl-sign-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="openssl_verify.html" class="function" rel="rdfs-seeAlso">openssl_verify()</a> - 验证签名</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>